#include "qmyNormativ.h"
#include "qnalogvnes.h"
#include "qnalogkorekcija.h"
#include "qnarackiWizard.h"

QMyNormativ::QMyNormativ(QWidget *parent)
    : QMyBaseWidget(parent)
    ,NormativVnes(0)
    ,NormativKorekcija(0)
    ,NormativLista(0)
    ,NormativWizard(0)
{
    ui.setupUi(this);
    pressF4();
}

QMyNormativ::~QMyNormativ()
{

}
void QMyNormativ::pressF2()
{
    closeAllForm();
    NormativVnes = showMyWidget<QNormativVnes>(NormativVnes); 
    NormativVnes->setCategoryWidget(this);
    connect(NormativVnes,SIGNAL(closeW()),this,SLOT(closeW()));
    connect(NormativVnes,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
    connect(NormativVnes,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
	emit disablePrint(true);
}
void QMyNormativ::pressF3()
{
    if(!NormativLista)
    {
        return;
    }
    int id = NormativLista->getSelectedId();
    closeAllForm();
    NormativKorekcija = showMyWidget<QNormativKorekcija>(NormativKorekcija); 
    NormativKorekcija->setCategoryWidget(this);
    connect(NormativKorekcija,SIGNAL(closeW()),this,SLOT(closeW()));
    connect(NormativKorekcija,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
    connect(NormativKorekcija,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
    NormativKorekcija->init(id);
	emit disablePrint(true);
}
void QMyNormativ::pressF4()
{
    closeAllForm();
    NormativLista = showMyWidget<QNormativLista>(NormativLista); 
    NormativLista->setCategoryWidget(this);
	connect(NormativLista,SIGNAL(sNormativToNalog(QString&, QString&)),this,SLOT(retNormativToNalog(QString&, QString&)), Qt::DirectConnection);

    connect(NormativLista,SIGNAL(closeW()),this,SLOT(closeWLista()));
    connect(NormativLista,SIGNAL(procF2()),this,SLOT(procF2()));
    connect(NormativLista,SIGNAL(procF3()),this,SLOT(procF3()));
    connect(NormativLista,SIGNAL(procF6()),this,SLOT(procF6()));
    connect(NormativLista,SIGNAL(procDel()),this,SLOT(procDel()));
	emit disablePrint(false);
}

void QMyNormativ::pressF6()
{
    if(!NormativLista)
    {
        return;
    }
    int id = NormativLista->getSelectedId();
    closeAllForm();
    NormativWizard = showMyWidget<QNormativWizard>(NormativWizard); 
    NormativWizard->setCategoryWidget(this);
    connect(NormativWizard,SIGNAL(closeW()),this,SLOT(closeW()));
    connect(NormativWizard,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
    connect(NormativWizard,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
    NormativWizard->init(id);
    emit disablePrint(true);
}

void QMyNormativ::procF2()
{
    pressF2();
}
void QMyNormativ::procF3()
{
    pressF3();
}
void QMyNormativ::procF6()
{
    pressF6();
}
void QMyNormativ::refresh()
{
}

void QMyNormativ::procDel()
{

}
void QMyNormativ::pressEscape()
{
    this->raise();
    this->setFocus();
	emit disablePrint(true);
    emit signCloseMyWidget();
}
void QMyNormativ::closeW()
{
    pressF4();
}
void QMyNormativ::closeWLista()
{
    pressEscape();
}

void QMyNormativ::closeAllForm()
{
    NormativKorekcija = deleteMyWidget<QNormativKorekcija>(NormativKorekcija);
    NormativVnes = deleteMyWidget<QNormativVnes>(NormativVnes);
    NormativLista = deleteMyWidget<QNormativLista>(NormativLista);
    NormativWizard = deleteMyWidget<QNormativWizard>(NormativWizard);
}
void QMyNormativ::callKomintenti(QWidget* myobj, QString text)
{
    emit signKomintentiFromIspratnica(myobj, text);
}

void QMyNormativ::callArtikli(QWidget* myobj, QString text)
{
    emit signArtikliFromIspratnica(myobj, text);
}

void QMyNormativ::sendPrintMessage()
{
    emit signPrint();
}


QStandardItemModel* QMyNormativ::getModel()
{
    QStandardItemModel * modelExt;
    if (NormativLista)
    {
        modelExt = NormativLista->model;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStandardItemModel* QMyNormativ::getModel2()
{
    QStandardItemModel * modelExt;
    if (NormativLista)
    {
        modelExt = NormativLista->model2;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStringList QMyNormativ::getIspratnicaInfo()
{
	QStringList modelExt;
	modelExt.clear();

	if (NormativLista)
	{
		modelExt = NormativLista->geInfo();
	} 
	else
	{
		modelExt;
	}
	return modelExt;
}


void QMyNormativ::retNormativToNalog(QString& text, QString& textName)
{
	QWidget *sour = getSourceWidget();
	if (qobject_cast<QNalogVnes*>(sour))
	{
		QNalogVnes *dest = qobject_cast<QNalogVnes*>(sour);
		dest->setArtikli(text, textName);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine();
	}
	if (qobject_cast<QNalogKorekcija*>(sour))
	{
		QNalogKorekcija *dest = qobject_cast<QNalogKorekcija*>(sour);
		dest->setArtikli(text, textName);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
    if (qobject_cast<QNarackiWizard*>(sour))
    {
        QNarackiWizard *dest = qobject_cast<QNarackiWizard*>(sour);
        dest->setArtikli(text, textName);
        emit eupdateNanigator(dest->getCategoryWidget(), this);
        dest->setFocusLine2();
    }

}